package com.example.administrator.zyfypt412lmy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.example.administrator.zyfypt412lmy.R;
import com.example.administrator.zyfypt412lmy.bean.LoginBean;
import com.example.administrator.zyfypt412lmy.iface.LoginListener;
import com.example.administrator.zyfypt412lmy.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnLog,btnReg;
    private EditText edtname,edtpass;
    private String username="",password="",sessionID="";//用户名密码
    private Switch sw;
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                sessionID=loginBean.getSessionid().toString();
                System.out.print("===sessionID"+sessionID);
                savaSp();
                Intent intent1 = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent1);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.print("fail");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void savaSp() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionID",sessionID);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b = sp.getBoolean("remember",false);
        System.out.print("===name"+name);
        if(b){
            edtname.setText(name);
            edtpass.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        btnLog=(Button)findViewById(R.id.button_log);
        btnReg=(Button)findViewById(R.id.button_reg);
        edtname=(EditText)findViewById(R.id.editText_log);
        edtpass=(EditText)findViewById(R.id.editText_pass);

        sw=(Switch)findViewById(R.id.switch1);
        btnReg.setOnClickListener(this);
        btnLog.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_reg:
                Intent intent = new Intent();
                intent.setClass(this, RegActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
            case R.id.button_log:
                System.out.println("点击登陆按钮");
                LoginModel loginModel = new LoginModel();
                username=edtname.getText().toString();
                password=edtpass.getText().toString();
                loginModel.getResult(username, password, loginListener);

                break;
        }
    }
}
